<?php

/**
 * Created by PhpStorm.
 * User: daimin
 * Date: 15/7/22
 * Time: 下午6:31
 */

namespace common\services\common;

use common\services\BaseService;
use common\entities\common\MyscrmOrganizationEntity;
use common\repositories\common\MyscrmOrganizationRepository;
use yii\helpers\ArrayHelper;
use common\support\PageParam;

/**
 * 租户操作
 * Class MyscrmOrganizationService
 * @package common\services\common
 */

class MyscrmOrganizationService extends BaseService {


    public $_organizationRepository;

    public function __construct(MyscrmOrganizationRepository$organizationRepository){
        $this->_organizationRepository = $organizationRepository;
        parent::__construct();
    }

    /**
     * 获取租户列表分页
     * @param string $keyword 搜索关键字
     * @param int $page 页码
     * @param int $page 页码
     * @param int $pagesize 每页大小
     * @param string $sortField 排序的字段
     * @param int $order 升序或降序，请使用系统常量
     * @return 结果
     */
    public function getOrganizationListByPage($keyword, $page=1, $pagesize=20, $sortField='FriendlyName', $order=SORT_ASC){
        $pageParam = new PageParam($page, $pagesize, $sortField, $order == SORT_ASC);
        return $this->_organizationRepository->getOrganizationListByPage($pageParam ,$keyword);
    }

    /**
     * 根据租户UniqueName(企业号)获取其ID
     * @param $orgName
     * @return string
     */
    public function getOrgId($orgName)
    {
        return $this->_organizationRepository->getOrgId($orgName);
    }


    /**
     * 根据租户ID得到租户信息
     * @param $id
     * @return null|static
     */
    public function getOrgById($id)
    {
        return $this->_organizationRepository->getById($id);
    }

    /**
     * 更新交付信息
     * @param $info
     * @return bool
     * @throws \Exception
     */
    public function updateOrganizationDeliverInfo($info){
        if(!array_key_exists('orgname', $info)){
            throw new \Exception("租户企业号不能为空");
        }

        return $this->_organizationRepository->updateOrganizationDeliverInfo($info);
    }

    /**
     * 获取租户列表（select2控件）
     * @param string $keyword 搜索关键字
     * @param string $limit 只显示几条记录
     * @return array
     */
    public function getOrganizationListForSelect2($keyword,$limit) {
        return $this->_organizationRepository->getOrganizationListForSelect2($keyword,$limit);
    }
}